<template>
  <div class="app-container">
    <el-card class="box-card">
      <div class="flex items-center flex-wrap">
        <el-input v-model="listQuery.user_code" class="mr-3" placeholder="用户编号" style="width: 200px;"></el-input>
        <el-input v-model="listQuery.nickname" class="mr-3" placeholder="用户昵称" style="width: 200px;"></el-input>
        <el-select v-model="listQuery.anchor_type" class="mr-3" clearable filterable placeholder="认证等级"
                   style="width: 200px;"
        >
          <el-option v-for="item in options"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value"
          >
          </el-option>
        </el-select>
        <el-button type="primary" @click="searchSubmit">查询</el-button>
        <el-button type="primary" @click="resetSubmit">重置</el-button>
      </div>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableData"
        border
        class="mt-3"
        stripe
        style="width: 100%"
        tooltip-effect="dark"
      >
        <el-table-column label="用户编号" prop="user_code"></el-table-column>
        <el-table-column label="用户昵称" prop="nickname"></el-table-column>
        <el-table-column label="认证时间" prop="audit_time"></el-table-column>
        <!--        <el-table-column label="认证等级" prop="streamer_level">-->
        <!--          <template slot-scope="{row}">-->
        <!--            <span>V{{ row.anchor_score }}</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="主播等级" prop="last_week_streamer_level">
          <template slot-scope="{row}">
            <span>{{ row.anchor_type_name }}</span>
          </template>
        </el-table-column>
        <!--        <el-table-column label="上周等级" prop="last_week_streamer_level">-->
        <!--          <template slot-scope="{row}">-->
        <!--            <span>V{{ row.last_week_anchor_level }}</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="本周活跃值" prop="anchor_score"></el-table-column>
        <el-table-column label="本周推荐量" prop="recommend_count"></el-table-column>
        <el-table-column label="本周收礼（积分）" prop="week_gift_total">
          <template slot-scope="{row}">
            <span>{{ row.week_gift_total }} 积分</span>
          </template>
        </el-table-column>
        <el-table-column label="累计收礼（积分）" prop="gift_total">
          <template slot-scope="{row}">
            <span>{{ row.gift_total }} 积分</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <!-- <el-button
              icon="el-icon-edit"
              plain
              size="mini"
              type="primary"
              @click="handleDetails(scope.row)"
            >查看
            </el-button> -->
            <el-button
              size="mini"
              type="text"
              @click="handleDetails(scope.row)"
            >查看资料
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleRecommend(scope.row)"
            >查看推荐
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="showEditLevelDialog(scope.row)"
            >修改主播等级
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="showBanDialog(scope.row)"
            >封禁主播
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <cus-pagination
        v-show="total > 0"
        :limit.sync="listQuery.perPage"
        :page.sync="listQuery.page"
        :total="total"
        @pagination="getTableData"
      />
    </el-card>

    <el-dialog
      :visible.sync="levelDialogVisible"
      title="修改主播等级"
      width="30%"
      @close="levelDialogVisible=false"
    >
      <el-form ref="form" :inline="false" :model="levleFormData" label-width="120px" size="normal">
        <el-form-item label="当前主播等级">
          <el-tag type="success">{{ levleFormData.anchor_type_name }}</el-tag>
        </el-form-item>
        <el-form-item label="修改主播等级">
          <el-select v-model="levleFormData.anchor_type" clearable filterable placeholder="" value-key="">
            <el-option v-for="item in options"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="levelDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleEditLevel">确定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :visible.sync="banDialogVisible"
      title="封禁理由"
      width="30%"
      @close="banDialogVisible=false"
    >
      <el-form ref="form" :inline="false" :model="levleFormData" label-width="120px" size="normal">
        <el-form-item label="理由">
          <el-input
            v-model="banForm.reason"
            placeholder="请输入封禁理由"
            type="textarea"
          />
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="banDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleBan">确定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { anchorTypeList, editLevel, listData } from '@/api/sys/anchorList'

export default {
  name: 'anchorList',
  components: {},
  data() {
    return {
      levelDialogVisible: false,
      banDialogVisible: false,
      levleFormData: {
        anchor_type: '',
        user_id: ''
        // anchor_type_name: ''
      },
      title: '',
      loading: false,
      form: {
        session: ''
      },
      banForm: {
        reason: ''
      },
      options: [
        { value: 1, label: '摩卡主播' },
        { value: 2, label: '优质主播' },
        { value: 3, label: '金牌主播' },
        { value: 4, label: '明星主播' }
      ],
      tableData: [
        {}
      ],
      listQuery: {
        page: 1,
        perPage: 10,
        user_code: undefined,
        nickname: undefined,
        anchor_type: undefined
      },
      total: 1,
      rules: {
        session: [{ required: true, message: '请选择面试场次', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getTableData()
    this.getAnchor()
  },
  methods: {
    getAnchor() {
      anchorTypeList().then(res => {
        this.options = res.data.map((item) => {
          item.label = item.anchor_type_name
          item.value = item.anchor_type
          return item
        })
      })
    },
    //
    onSubmit() {
      this.$refs['newHeadlines'].validate(valid => {
        if (valid) {

        }
      })
    },

    // 网络请求
    /**
     * getTableData
     * @desc 获取表格数据
     */
    async getTableData() {
      this.loading = true
      for (let key in this.listQuery) {
        this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key]
      }
      const detail = await listData(this.listQuery)
      if (detail.code === 200) {
        this.tableData = detail.data.data
        this.listQuery.page = detail.data.current_page
        this.total = detail.data.total
        this.listQuery.perPage = Number(detail.data.per_page)
      }
      this.loading = false

    },
    closeDialog(form) {
      if (this.$refs[form]) {
        this.$refs[form].clearValidate() // 清除整个表单的校验
        this.$refs[form].resetFields() // 清除整个表单的校验
      }
    },
    searchSubmit() {
      this.listQuery.page = 1
      this.getTableData()
    },
    resetSubmit() {
      this.listQuery = {
        page: 1,
        perPage: 10
      }
      this.getTableData()
    },
    handleDetails(row) {
      this.$router.push({
        path: 'anchorDetails',
        query: {
          user_id: row.user_id
        }
      })
    },
    handleRecommend(row) {
      this.$router.push({
        path: '/anchorManagement/recommendList',
        query: {
          user_id: row.user_id
        }
      })
    },
    showEditLevelDialog(row) {
      this.levelDialogVisible = true
      this.levleFormData.user_id = row.user_id
      this.levleFormData.anchor_type = row.anchor_type
      this.levleFormData.anchor_type_name = row.anchor_type_name
    },
    showBanDialog(row) {
      this.banDialogVisible = true
      this.levleFormData.user_id = row.user_id
    },
    async handleEditLevel() {
      this.loading = true
      const detail = await editLevel(this.levleFormData)
      if (detail.code === 200) {
        this.$message.success('修改成功！')
        this.getTableData()
      }
      this.loading = false
      this.levelDialogVisible = false
    },
    async handleBan() {
      if (!this.banForm.reason) return this.$message.warning('请输入理由!')
      this.loading = true
      const detail = await editLevel(this.levleFormData)
      if (detail.code === 200) {
        this.$message.success('修改成功！')
      }
      this.loading = false
      this.levelDialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
